# ***********************************************************************
# autodoc, a tool to grab & generate html version of django doc         *
#                                                                       *
# Copyright (c) 2006                                                    *
# Nicolas Steinmetz - <nsteinmetz@gmail.com> -                          *
# http://Www.unelectronlibre.info                                       *
#                                                                       *
# This program is released under the GNU/GPL License.                   *
# For more details, read COPYING.txt                                    *
#                                                                       *
# Ce programme est un logiciel libre distribue sous licence GNU/GPL.    *
# Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.  *
# ***********************************************************************

import os
import pysvn
from docutils.core import publish_file

# Definition of useful variables
dirdoc = 'docs'

# Let's do a svn checkout on docs
client = pysvn.Client()
client.checkout('http://code.djangoproject.com/svn/django/trunk/docs/', '%s' % (dirdoc))


# We go into the repertory where django docs are stored
os.chdir("%s" % (dirdoc))

# If there are some .html files, we remove them
os.system('rm -f *.html')

# We list remaining files (should be only .txt files)
filelist = os.listdir("./")

# We "remove" .svn directory from list
del filelist[0]

# We sort the list, and for each element of the list, we transform .txt file into .html one
filelist.sort()
for file in filelist:
    # we want to get filename.txt <-> filename.html - so let's build it by removing .txt extension and add .html one
    max = len(file)-4
    filename = file[:max]+'.html'
    # let's build the .html file
    publish_file(source_path=file, destination_path=filename, writer_name='html')
    print htmlfilename,' generated'

